import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie



NProgress.configure({ showSpinner: false }) // NProgress Configuration
const whiteList = ['/login', '/404', '/405', '/test/home'] // no redirect whitelist



router.beforeEach(async(to, from, next) => {

    // start progress bar
    NProgress.start()
    console.log(to.path)

    // determine whether the user has logged in
    const hasToken = getToken()

    if (whiteList.indexOf(to.path) !== -1) {
        // in the free login whitelist, go directly
        next()
        NProgress.done()
    } else {
        if (hasToken) {
            console.log(hasToken)
            next()
        } else {
            next('/login')
            NProgress.done()
        }


    }
})

router.afterEach(() => {
    // finish progress bar
    NProgress.done()
})